const { ordersModel } = require('./models/ordersModel')
const { usersModel } = require('./models/usersModel')
const { mongoose } = require('mongoose')

module.exports.getAllOrders = async ({ currentPage, pageSize, type = "name", value = '' }) => {
    // console.log(currentPage);
    const total = await ordersModel.countDocuments({ isDel: false, });
    const totalPages = Math.ceil(total / pageSize)
    const data = await ordersModel
        .find({ isDel: false })
        .populate({
            path: 'insuresId usersId personalId'
        })
        .limit(pageSize - 0)
        .skip((currentPage - 1) * pageSize)
        console.log(data);
    return { total, totalPages, data }
}

//获取退保订单
module.exports.getSurrenderOrders = async () => {
    return await ordersModel.find({ cancellation: { $gt: 0 } })
    .populate({
        path: 'insuresId usersId personalId'
    })
}

module.exports.delOrdersById = async params => {
    return await ordersModel.updateOne(params, { isDel: true })
}
//同意退保
module.exports.handelSurrenderOrders = async params => {
    return await ordersModel.updateOne(params, { cancellation: 2 })
}
//拒绝退保
module.exports.refuseSurrenderOrders = async params => {
    return await ordersModel.updateOne(params, { cancellation: 3 })
}

module.exports.getOneOrderById = async params => {
    return await ordersModel.find(params).populate({
        path: 'insuresId usersId salespersonsId'
    })
}

module.exports.getSaleManOrder = async ({ _id = '60378126d12900002c0042f3' } = {}) => {
    return await ordersModel.find({ personalId: _id }).populate({
        path: 'insuresId usersId salespersonsId'
    })
}

module.exports.updateOrderBySaleMan=async (params)=>{
    return await ordersModel.updateMany({ _id: params._id }, params)
}
//客户下单
module.exports.addOrder=async params=>{
    return await ordersModel.create(params)
}